package tmi.li.testapp;

import java.util.ArrayList;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;




public class LayoutEditor extends SurfaceView implements SurfaceHolder.Callback {
	
	public CanvasThread canvasthread;
	private LayoutElementList lelist = new LayoutElementList();
	int index; 
	boolean isTouched = false;
	public Paint mPaint; 

	Bitmap _trash;
	
	public LayoutEditor(Context context, AttributeSet attrs) {
		super(context, attrs);
		getHolder().addCallback(this);
	    canvasthread = new CanvasThread(getHolder(), this);
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setColor(0xFFDDDDDD);
        mPaint.setTextSize((float)30);
        _trash = BitmapFactory.decodeResource(context.getResources(), R.drawable.trash);
	    setFocusable(true);
	}


	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}


	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		canvasthread.setRunning(true);
        canvasthread.start();   
	}


	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
    	boolean retry = true;
        canvasthread.setRunning(false);
        while (retry) {
        	try {
        		canvasthread.join();
        		retry = false;
        	} catch (InterruptedException e) {
                             // we will try it again and again...
        	}
        }
	}

	
	
	@Override
    public boolean onTouchEvent(MotionEvent event) {
        synchronized (canvasthread.getSurfaceHolder()) {
        	Coordinates coords;
        	
        	if(event.getAction() == MotionEvent.ACTION_DOWN){
        		
        		isTouched = true;
        		if(!lelist.match((int)event.getX(), (int)event.getY())){
        			lelist.resetSelected();
        		}

        		
        	}else if(event.getAction() == MotionEvent.ACTION_MOVE){
        		
        		isTouched = true;
        		if(lelist.matchSelected((int)event.getHistoricalX(0), (int)event.getHistoricalY(0))) {
        			lelist.moveSelected((int)event.getX(), (int)event.getY());
        		}


        	}else if(event.getAction() == MotionEvent.ACTION_UP){
        		
        		isTouched = false;
        		lelist.remove(this.getWidth(), this.getHeight());
        		
        	}
        	
        	return true;
        }
	}

	
	public void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		drawCoordinateSystem(canvas);

		if(isTouched && lelist.hasSelected ) {
			
			canvas.drawBitmap(_trash, this.getWidth()/2-16,this.getHeight()-80,null);
		}
		
		lelist.draw(canvas);
	}
	
	public void drawCoordinateSystem(Canvas canvas) {
		canvas.drawLine(10, 10, this.getWidth(), 10, mPaint);
	}
	
	public void update() {
		
	}

    public void addElement() {
    	LayoutElement obj = new LayoutElement();
    	obj.getCoords().setX(20);
    	obj.getCoords().setY(40);
    	lelist.add(obj);
    }
}